package com.ll.daipao.wechat.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ll.daipao.handler.Constants;
import com.ll.daipao.wechat.service.OrderService;
import io.swagger.annotations.*;
import netscape.javascript.JSObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by rockyll on 2017/10/13.
 */
@Controller
@RequestMapping("order")
public class OrderController extends BaseController{

    @Autowired
    OrderService orderService;

    @RequestMapping(value = "list",produces="application/json;charset=UTF-8",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="获取所有发布订单", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页面展示条数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "type", value = "订单类别", required = true, dataType = "String")
    })
    public String list(HttpServletRequest request){
        String pageSize = request.getParameter("pageSize");
        String pageNum = request.getParameter("pageNum");
        int size = 10,num = 1;
        if(StringUtils.isNotBlank(pageSize)){
            size = Integer.parseInt(pageSize);
        }

        if(StringUtils.isNotBlank(pageNum)){
            num = Integer.parseInt(pageNum);
        }

        String type = request.getParameter("type");
        Map<String,Object> map = new HashMap<>();
        map.put("type",type);
        PageHelper.startPage(num,size);
        List<Map<String, Object>> maps = orderService.selectAll(map);
        PageInfo info = new PageInfo(maps);
        return success(info);
    }




    /**
     * 接单
     * @param request
     * @return
     */
    @RequestMapping(value = "reOrder",produces="application/json;charset=UTF-8")
    @ResponseBody
    public String reOrder(HttpServletRequest request){
        String openId = (String) request.getSession().getAttribute(Constants.WX_OPENID);
        String id = request.getParameter("id");
        if(StringUtils.isBlank(id)){
           return failed("缺失重要数据！");
        }
        try {
            JSONObject res = orderService.reOrder(openId,id);
            if(res.getBoolean("suc")){
                return success();
            }
            else {
                return failed(res.getString("msg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return failed();
        }
    }

    /**
     * 订单详情
     * @param request
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "taskDetail/{id}")
    public String taskDetail(HttpServletRequest request, Model model, @PathVariable Integer id){
       String opendid= (String) request.getSession().getAttribute(Constants.WX_OPENID);
        model.addAllAttributes(orderService.selectDetail(id,opendid));
        return "taskDetail";
    }



    @RequestMapping(value = "mineObtainOrder")
    public String mineObtainOrder(HttpServletRequest request){
        return "mineObtainOrder";
    }


    @RequestMapping(value = "mineObtainOrderlist",produces = "application/json;charset=UTF-8")
    @ApiOperation(value="我的发单", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSiz", value = "页面展示条数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "type", value = "订单类别", required = true, dataType = "String")
    })
    @ResponseBody
    public String mineObtainOrderlist(HttpServletRequest request){
        String opendid= (String) request.getSession().getAttribute(Constants.WX_OPENID);
        String pageSize = request.getParameter("pageSiz");
        String pageNum = request.getParameter("pageNum");
        int size = 10,num = 1;
        if(StringUtils.isNotBlank(pageSize)){
            size = Integer.parseInt(pageSize);
        }

        if(StringUtils.isNotBlank(pageNum)){
            num = Integer.parseInt(pageNum);
        }

        String type = request.getParameter("type");
        Map<String,Object> map = new HashMap<>();
        map.put("type",type);
        PageHelper.startPage(num,size);
        List<Map<String, Object>> maps = orderService.selectAllHeOrderByMap(map);
        PageInfo info = new PageInfo(maps);
        return success(info);
    }



    @RequestMapping(value = "mineOrder")
    public String mineOrder(HttpServletRequest request){
        return "mineOrder";
    }


    @RequestMapping(value = "mineOrderList",produces = "application/json;charset=UTF-8")
    @ApiOperation(value="我的接单", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSiz", value = "页面展示条数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "type", value = "订单类别", required = true, dataType = "String")
    })
    @ResponseBody
    public String mineOrderList(HttpServletRequest request){
        String opendid= (String) request.getSession().getAttribute(Constants.WX_OPENID);
        String pageSize = request.getParameter("pageSiz");
        String pageNum = request.getParameter("pageNum");
        int size = 10,num = 1;
        if(StringUtils.isNotBlank(pageSize)){
            size = Integer.parseInt(pageSize);
        }

        if(StringUtils.isNotBlank(pageNum)){
            num = Integer.parseInt(pageNum);
        }

        String type = request.getParameter("type");
        Map<String,Object> map = new HashMap<>();
        map.put("type",type);
        PageHelper.startPage(num,size);
        List<Map<String, Object>> maps = orderService.selectAllReOrderByMap(map);
        PageInfo info = new PageInfo(maps);
        return success(info);
    }


    @RequestMapping(value = "mineProfit")
    public String mineProfit(HttpServletRequest request){
        String opendid= (String) request.getSession().getAttribute(Constants.WX_OPENID);
        List<Map<String, Object>> maps = orderService.selectAllMoney(opendid);
        if(maps!=null&&maps.size()>0){
            BigDecimal sums = new BigDecimal(0);
            for(Map<String,Object> map : maps){
                BigDecimal amount = new BigDecimal(map.containsKey("amount")?String.valueOf(map.get("amount")):"0");
                sums = sums.add(amount);
            }
            request.setAttribute("sums",sums);
        }
        request.setAttribute("orderlist",maps);
        return "mineProfit";
    }

    @RequestMapping(value = "myOrderList")
    public String myOrder(HttpServletRequest request){
        return null;
    }




}
